package com.lanchetech.admin.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.lanchetech.admin.service.TeacherService;
import com.lanchetech.bean.request.IdentityReq;
import com.lanchetech.bean.request.TeacherGroupReq;
import com.lanchetech.bean.response.BasePageResp;
import com.lanchetech.bean.response.BaseResp;
import com.lanchetech.bean.response.ResultData;
import com.lanchetech.bean.util.PageHelpUtil;
import com.lanchetech.bean.vo.IdentityVO;
import com.lanchetech.bean.vo.TeacherGroupVO;
import com.lanchetech.common.enums.ResultCode;
import com.lanchetech.common.enums.TeacherLevelEnum;
import com.lanchetech.common.enums.UserLevelEnum;
import com.lanchetech.dao.IdentityMapper;
import com.lanchetech.dao.TeacherGroupMapper;
import com.lanchetech.dao.UserMapper;
import com.lanchetech.entity.Identity;
import com.lanchetech.entity.User;
import com.lanchetech.service.DaoService;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class TeacherServiceImpl implements TeacherService {

    @Autowired
    IdentityMapper identityMapper;

    @Autowired
    UserMapper userMapper;

    @Autowired
    DaoService daoService;

    @Autowired
    TeacherGroupMapper teacherGroupMapper;


    @Override
    public BaseResp addTeacher(String phone, Byte teacherLevel) {
        User user = userMapper.findOneByUsername(phone);
        if (user == null) {
            return new BaseResp(ResultCode.ACCT_NOT_EXIST);
        }
        Identity identity = identityMapper.findOneByUserId(user.getId());
        if (identity == null) {
            return new BaseResp(ResultCode.IDENTITY_NOT_EXIST);
        }
        if (!TeacherLevelEnum.DEFAULT.getLevel().equals(teacherLevel)) {
            if (identity.getLevel() < UserLevelEnum.PARTNER_L1.getLevel()) {
                return new BaseResp(ResultCode.IDENTITY_NOT_REACH_PARTNER);
            }
        }
        identityMapper.updateTeacherLevelById(teacherLevel, identity.getId());

        // 然后设置此人成为讲师团队的一员

        return new BaseResp();
    }

    @Override
    public BaseResp removeTeacher(Long id) {
        identityMapper.updateTeacherLevelById(TeacherLevelEnum.DEFAULT.getLevel(), id);
        return new BaseResp();
    }

    @Override
    public ResultData<BasePageResp<IdentityVO>> getTeacherPage(IdentityReq req) {
        Page page = PageHelper.startPage(req.getPageNo(), req.getPageSize());
        List<IdentityVO> list = identityMapper.findAllTeacher(req);
        if (!CollectionUtils.isEmpty(list)) {
            Map<Long, User> userMap = daoService.getUserMap(list.stream().map(IdentityVO::getUserId).collect(Collectors.toList()));
            list.stream().forEach(item -> {
                item.setNickname(userMap.get(item.getUserId()).getNickname());
                item.setAvatar(userMap.get(item.getUserId()).getAvatar());
                item.setPhone(userMap.get(item.getUserId()).getUsername());
            });
        }
        return PageHelpUtil.buildPage(list, page);
    }

    @Override
    public ResultData<BasePageResp<TeacherGroupVO>> getTeacherGroupPage(TeacherGroupReq req) {
        Page page = PageHelper.startPage(req.getPageNo(), req.getPageSize());
        List<TeacherGroupVO> list = teacherGroupMapper.findAllPage(req);
        if (!CollectionUtils.isEmpty(list)) {
            List<Long> captionIds = list.stream().map(TeacherGroupVO::getCaptainId).collect(Collectors.toList());
            List<Long> memberIds = list.stream().map(TeacherGroupVO::getMemberId).collect(Collectors.toList());
            captionIds.addAll(memberIds);
            Map<Long, User> userMap = daoService.getUserMap(captionIds);
            list.stream().forEach(item -> {
                item.setCaptainNickname(userMap.get(item.getCaptainId()).getNickname());
                item.setCaptainAvatar(userMap.get(item.getCaptainId()).getAvatar());
                item.setMemberAvatar(userMap.get(item.getMemberId()).getAvatar());
            });
        }
        return PageHelpUtil.buildPage(list, page);
    }
}
